package src.main.java.controller;


import annotation.Log;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import src.main.java.service.SysRoleService;
import vo.common.dto.system.SysRoleDto;
import vo.common.entity.system.SysRole;
import vo.common.vo.common.Result;
import vo.common.vo.common.ResultCodeEnum;

import java.util.Map;

@Tag(name = "角色管理接口")
@RestController
@RequestMapping(value = "/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService ;

    @Log(title = "角色管理:添加",businessType = 1)
    @Operation(summary = "角色添加接口")
    @PostMapping(value = "/saveSysRole")
    public Result saveSysRole(@RequestBody SysRole SysRole) {
        sysRoleService.saveSysRole(SysRole) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * ***** 分页查询 - PageHelper
     *
     *      过程
     *          1、在mybatis-config.xml中配置
     *          2、url中传入当前页、每页数量
     *          3、在PageHelper设置分页参数
     *          4、查出符合条件的全部数据，然后页码对应的的数据
     *
     *      特点
     *          pageHelper是假分页
     *          会将全部数据查出来，然后根据设置的参数取需要的数据
     *
     */
    // pageNum：当前页
    // pageSize：每一页显示的条数
    @Operation(summary = "获取角色列表接口")
    @PostMapping("/findByPage/{current}/{limit}")
    public Result<PageInfo<SysRole>> findByPage(@RequestBody SysRoleDto sysRoleDto ,
                                                @PathVariable(value = "current") Integer current ,
                                                @PathVariable(value = "limit") Integer limit) {
        PageInfo<SysRole> pageInfo = sysRoleService.findByPage(sysRoleDto , current , limit) ;
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "提交修改接口")
    @PutMapping(value = "/updateSysRole")
    public Result updateSysRole(@RequestBody SysRole sysRole) {
        sysRoleService.updateSysRole(sysRole) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "删除接口")
    @DeleteMapping(value = "/deleteById/{roleId}")
    public Result deleteById(@PathVariable(value = "roleId") Long roleId) {
        sysRoleService.deleteById(roleId) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * ***** url参数的获取
     *
     *          动态参数如userId可以通过{}括起来进行传输
     *
     *          前端需要手动传入参数userId，代码如下 export const GetRoleList() = (userId)=>{
     *              return request(
     *                  url:'/admin/system/sysRole/findAllRoles/{userId}'
     *              )
     *          }
     *
     *          后端通过@PathVariable注解进行获取
     *
     */
    @Operation(summary = "指定用户的角色数据")
    @GetMapping(value = "/findAllRoles/{userId}")
    public Result<Map<String , Object>> findAllRoles(@PathVariable(value = "userId") Long userId) {
        Map<String, Object> resultMap = sysRoleService.findAllRoles(userId);
        return Result.build(resultMap , ResultCodeEnum.SUCCESS)  ;
    }


}
